package com.mobile.framework.util;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class Pagination<E> implements Serializable {
	protected final Log logger = LogFactory.getLog(this.getClass());
	
	// 一共有多少条记录
	private int totalCount = 0;

	// 每页显示多少条记录
	private int count = 20;

	// 当前第几页
	private int currentPage = 1;

	// 记录集
	private List<E> pageResult;

	private int totalPage = 1;

	private boolean hasNextPage = false;

	private boolean hasPrePage = false;

	public Pagination() {

	}

	public Pagination(int count, int currentPage) {
		this.setCount(count);
		this.setCurrentPage(currentPage);
	}

	public static Pagination makePagination() {
		return new Pagination();
	}

	public static Pagination makePagination(int count, int currentPage) {
		return new Pagination(count, currentPage);
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		if (count < 1) {
			this.count = 20;
		} else {
			this.count = count;
		}
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 设置当前页
	 * 
	 * @param currentPage
	 */
	public void setCurrentPage(int currentPage) {
		if (currentPage <= 0) {
			this.currentPage = 1;
		} else {
			this.currentPage = currentPage;
		}
	}

	/**
	 * 是否还有下一页
	 * 
	 * @return
	 */
	public boolean hasNextPage() {
		this.hasNextPage = this.currentPage < this.getTotalPage() ? true : false;
		return this.hasNextPage;
	}

	/**
	 * 是否还有上一页
	 * 
	 * @return
	 */
	public boolean hasPrePage() {
		this.hasPrePage = this.currentPage > 1 ? true : false;
		return this.hasPrePage;
	}

	/**
	 * 获得取记录的起始位置
	 * 
	 * @return
	 */
	public int getStartIndex() {
		int startIndex = (this.currentPage - 1) * count;
		return startIndex;
	}

	/**
	 * 获得总的页数
	 * 
	 * @return
	 */
	public int getTotalPage() {
		this.totalPage = this.totalCount / count;
		if ((totalPage * count) < this.totalCount || this.totalCount == 0) {
			this.totalPage++;
		}
		return this.totalPage;
	}

	/**
	 * 返回一页的记录集
	 * 
	 * @return
	 */
	public List<E> getPageResult() {
		return pageResult;
	}

	/**
	 * 设置一页里面的记录集
	 * 
	 * @param pageResult
	 */
	public void setPageResult(List<E> pageResult) {
		this.pageResult = pageResult;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("页次:");
		sb.append("<font color='red'>");
		sb.append(this.currentPage);
		sb.append("</font>");
		sb.append("/");
		sb.append("<font color='red'>");
		sb.append(this.getTotalPage());
		sb.append("</font>");
		sb.append("共");
		sb.append("<font color='red'>");
		sb.append(this.totalCount);
		sb.append("</font>");
		sb.append("条记录");
		return sb.toString();
	}

	public String toWapString() {
		StringBuffer sb = new StringBuffer();
		if (this.getTotalPage() > 1) {
			sb.append("页次:");
			sb.append(this.currentPage);
			sb.append("/");
			sb.append(this.getTotalPage());
			sb.append("<br/>");
		}
//		if (this.currentPage < this.totalPage) {
//			String nextpage = "<a href=\"?page=" + (this.currentPage + 1)
//					+ "\">下一页</a>";
//			sb.append(nextpage);
//		}
//		if (this.currentPage > 1) {
//			String prepage = "<a href=\"?page=" + (this.currentPage - 1)
//					+ "\">上一页</a>";
//			sb.append(prepage);
//		}
//		if (this.totalPage > 5) {
//			sb.append("<br/>");
//			sb.append("<input type=\"text\" name=\"page\" emptyok=\"true\" value=\""
//					+ this.currentPage + "\" size=\"4\" format=\"*N\"/>");
//			sb.append("<anchor>");
//			sb.append("GO");
//			sb.append("<go href=\"?\" method=\"post\">");
//			sb.append("<postfield name=\"page\" value=\"$(page)\" />");
//			sb.append("</go>");
//			sb.append("</anchor>");
//			sb.append("<br/>");
//		}
		return sb.toString();
	}
}
